﻿using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Clock
{
    public partial class NoteForm : Form
    {
        public NoteForm()
        {
            InitializeComponent();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private string key = "j0ZGxhVYRCdnQELUVcWy0FAQNgB35xB4";

        protected override void OnVisibleChanged(System.EventArgs e)
        {
            base.OnVisibleChanged(e);
            if(Visible)
            {
                if (File.Exists(Path.Combine(Application.StartupPath, "备忘.txt")))
                {
                    TripleDes des = new TripleDes {KeyInBase64 = key};
                    byte[] content=des.Decrypt(File.ReadAllBytes(Path.Combine(Application.StartupPath, "备忘.txt")));
                    NoteTextBox.Text = Encoding.UTF8.GetString(content);
                    NoteTextBox.SelectionStart = 0;
                    NoteTextBox.SelectionLength = 0;

                }
            }else
            {
                TripleDes des = new TripleDes { KeyInBase64 = key };
                File.WriteAllBytes(Path.Combine(Application.StartupPath, "备忘.txt"), des.Encrypt(Encoding.UTF8.GetBytes(NoteTextBox.Text))); 
            }
        }
    }
}
